decode([],[]):-!.
decode(L1,L2):-
    decode(L1,L2,[]).
    
decode([], L2, L2).
decode([X|T],L2,Acc):-
    sub_decode(X,R,[]),
    append(Acc,R,Acc1),
    decode(T,L2,Acc1).

sub_decode([2,A],L2,Acc):-!,
    append(Acc,[A,A],Acc1),
    L2 = Acc1.
sub_decode([N,A],L2,Acc):-!,
    append(Acc,[A],Acc1),
    N1 is N-1,
    sub_decode([N1,A],L2,Acc1).
sub_decode(A,[A],_).